let domain = "oocl.com";

async function loaded() {
    const cookie = document.getElementById('cookie')
    const delCookie = document.getElementById('delCookie')
    const setCookie = document.getElementById('setCookie')
    const getCookie = document.getElementById('getCookie')

    delCookie.addEventListener('click', event => {
        chrome.tabs.query({}, function (tabs) {
            for (let i = 0; i < tabs.length; i++) {
                const tab = tabs[i]
                if (tab.url.indexOf(domain) != -1) {
                    chrome.tabs.sendMessage(tab.id, {type: 'delCookie'}, function (response) {
                        console.log('delCookie')
                    });
                }
            }
        });
    })

    setCookie.addEventListener('click', event => {
        chrome.tabs.query({}, function (tabs) {
            for (let i = 0; i < tabs.length; i++) {
                const tab = tabs[i]
                if (tab.url.indexOf(domain) != -1) {
                    chrome.tabs.sendMessage(tab.id, {type: 'setCookie', otherCache: cookie.value}, function (response) {
                        console.log('setCookie')
                    });
                }
            }
        });
    })

    getCookie.addEventListener('click', event => {
        chrome.tabs.query({}, function (tabs) {
            for (let i = 0; i < tabs.length; i++) {
                const tab = tabs[i]
                if (tab.url.indexOf(domain) != -1) {
                    chrome.tabs.sendMessage(tab.id, {type: 'getCookie'}, function (response) {
                        console.log('getCookie')
                    });
                }
            }

            let id = setInterval(async function () {
                let cookies = await session.get("cookies", null);
                let localStorage = await session.get("localStorage", null);
                if (!!cookies && !!localStorage) {
                    clearInterval(id);

                    var _cookies = []
                    for (let cookie of cookies) {
                        if (cookie.name != 'HMY_JC'
                            && cookie.name != 'HBB_HC'
                            && cookie.name != 'HMF_CI'
                            && cookie.name != 'CSH_DF'
                            && cookie.name != 'CSH_UF'
                        ) {
                            _cookies.push(cookie)
                        }
                    }

                    let otherCache = JSON.stringify({
                        localStorage: localStorage,
                        cookies: _cookies
                    });

                    cookie.value = JSON.stringify(otherCache)

                }
            }, 2000)

        });
    })
}

document.addEventListener('DOMContentLoaded', loaded)